<?php

namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;

class GoodsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        //秒杀产品
        if ($this->is_seckill){
            if (!$this->flash_sale_price){
                $price = "";
            }else{
                $price = $this->flash_sale_price;
            }
            $sku = $this->flash_sale_number;
        }else{
            $sku = $this->sku?$this->sku:"N";
            if ($this->integral>0){
                if (!$this->price){
                    $price = "";
                }else{
                    $price = $this->price;
                }
            }else{
                if (!$this->price){
                    $price = "面议";
                }else{
                    $price = $this->price;
                }
            }
        }

        return [
            "id"=>$this->id,
            "name"=>$this->name,
            "long_title"=>$this->long_title,
            "img"=>get_img_url($this->img),
            "is_new"=>$this->is_new,
            "is_hot"=>$this->is_hot,
            "is_recommend"=>$this->is_recommend,
            "is_seckill" =>$this->is_seckill,
            "on_sale"=>$this->on_sale ? "在售":"已下架",
            "price"=>$price,
            "promote_price"=>$this->promote_price,
//            "sku"=>$this->sku,
            "sku"=>$sku,
            "limit_number"=>$this->limit_number,
//            "flash_sale_price"=>$this->flash_sale_price,
            "flash_sale_start_time"=>$this->flash_sale_start_time,
            "flash_sale_end_time" => $this->flash_sale_end_time,
//            "flash_sale_number"=>$this->flash_sale_number,
            "integral"=>$this->integral,//兑换所需积分

            "spec"=>$this->spec,
            "tags"=>$this->tags?explode(",",$this->tags):"",
            "stars"=>$this->stars,
            "goodsCategory"=>new GoodsCategoryResource($this->whenLoaded("goodsCategory")),
            "authors"        =>new AuthorResource($this->whenLoaded("authors")),
            "goodsImages"  =>new GoodsImagesResource($this->whenLoaded("goodsImages")),
            "goodsContent" =>new GoodsContentResource($this->whenLoaded("goodsContent")),
        ];

//        return parent::toArray($request);
    }
}
